define(["jquery","bbcare","fu/app/comm/comm"],function($,$bb,$comm){
	 var URL = $bb.baseUrl("followup");
	var upload = {};
	upload.minFile = function(){
		var _args = arguments;
		var params = _args[0];
		var typeStr = "image/jpg, image/png, image/gif, image/jpeg";
	    var files = params.files;
	    if(files){
	    	for (var i = 0; i < files.length; i++) {
	    		if(typeStr.indexOf(files[i].type) == -1){
	    			$bb.toastShow("请上传格式为jpg、png、gif、jpeg的图片！");
			        return;
			    }
			    if (files[i].size >= 1024*1024*10) {
			    	$bb.toastShow("上传的文件不能大于10M！");
			        return;
			　　}
			    params.file = files[i];
		    	/*----------上传到oss--------------------*/
		    	upload.uploadEducationLogo(params,function(data,params){
		    		params.imgUrl = data.filePathList[0];
		    		if($.isFunction(_args[1])){
		    			_args[1](params)
		    		}else{
		    			$bb.toastShow("上传成功");
		    		}
		    	})
	    	}
	    }else{
	    	$bb.toastShow("请选择上传的图片");
	    }
	}
	upload.maxFile = function(){
		var _args = arguments;
		var params = _args[0];
		var typeStr = "image/jpg, image/png, image/gif, image/jpeg";
	    var files = params.files;
	    if(files){
	    	for (var i = 0; i < files.length; i++) {
	    		if(typeStr.indexOf(files[i].type) == -1){
			        $bb.toastShow("请上传格式为jpg、png、gif、jpeg的图片！");
			        return;
			    }
			    if (files[i].size >= 1024*1024*10) {
			        $bb.toastShow("上传的文件不能大于10M！");
			        return;
			　　}
			    params.file = files[i];
		    	/*----------上传到oss--------------------*/
		    	upload.ajaxPolicy({"file":files[i]},function(policy,params){
		    		var fd = new FormData();
			    	var filename = $comm.random(5) + $comm.timestamp() + $comm.getSuffix(params.file.name);
			        fd.append('key', policy.dir + filename);  //存储在oss的文件路径
			        fd.append('policy', policy.policy);  //policy
			        fd.append('OSSAccessKeyId', policy.accessId);  //accessKeyId
			        fd.append('success_action_status', "200");  //成功后返回的操作码
			        fd.append('Signature', policy.signature);   //签名
			        fd.append("file",params.file);
			        $.extend(params,{
			        	"url":policy.host,
			        	"fd":fd,
			        	"imgUrl":policy.host + policy.dir + filename,
			        });
			        upload.ajaxServce(params,function(result,params){
			        	if($.isFunction(_args[1])){
			    			_args[1](params)
			    		}else{
			    			$bb.toastShow("上传成功");
			    		}
			        })
		    	},params);
	    	}
	    }else{
	    	$bb.toastShow("请选择上传的图片");
	    }
	}
	/***********获取oss签名信息*****************/
	upload.ajaxPolicy = function(args,callback,params){
		$bb.ajax({
            url: URL + "followapp/aliyun/oss/getPolicy",
            data: JSON.stringify({
                data: args
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,params);
                }else{
                   $bb.toastShow(data.msg ? data.msg : "请求policy出错");
                }
            }
        });
	}
	/***********上传到oss*****************/
	upload.ajaxServce = function(params,callback){
		var xhr = new XMLHttpRequest();
        xhr.open('post',params.url,true);
        xhr.onreadystatechange=function (){
            if(this.readyState==4){
                callback(this.status,params);
            }
        }
        xhr.upload.onprogress=function (ev){
            if(ev.lengthComputable){
                var precent = 100 * ev.loaded / ev.total;
            }
        }
        xhr.send(params.fd);
	}
	/***********上传小型图片不需sessionId*****************/
	upload.uploadEducationLogo = function(params,callback){
		var fd = new FormData();
        fd.append("file", params.file);//上传的文件： 键名，键值
		$bb.ajax({
		  	url: URL + "followapp/webapi/service/system/applyUpload",
		  	processData: false,  // 告诉jQuery不要去处理发送的数据
		  	contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
		  	data: fd,
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					 $bb.toastShow(data.msg ? data.msg :"上传图片失败！");
				}
			}
		});
	}
	return upload;
});
